import 'package:flutter/material.dart';
import 'package:netstorage/model/CommonModel.dart';
// 首页 活动入口
class SubNavWidget extends StatelessWidget {
  final List<CommonModel>? subNavList;
  const SubNavWidget({super.key,this.subNavList});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(7,0,7,4),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(6)),
      child: Padding(
        padding: const EdgeInsets.all(7),
        child: _items(context),
      ),
    );
  }

  _items(BuildContext context) {
    if(subNavList == null || subNavList?.length == 0) return null;
    List<Widget> items = [];
    for(var model in subNavList!){
      items.add(_item(context,model));
    }
    // 拆分为两个数组的拆解点index
    int index = (subNavList!.length / 2 + 0.5).toInt();
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: items.sublist(0,index),
        ),
        Padding(
          padding: EdgeInsets.only(top: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: items.sublist(index,subNavList!.length),
          ),
        ),
      ],
    );
  }

  Widget _item(BuildContext context, CommonModel model) {
    return Expanded(child: GestureDetector(
      child: Column(
        children: [
          Image.network(model.icon!, width: 18, height: 18,),
          Padding(
            padding: EdgeInsets.only(top: 3),
            child: Text(model.title!, style: TextStyle(fontSize: 12))
          )
        ],
      ),
    ));
  }
}
